package serialComm;

import java.io.*;  
import java.util.*;  
import javax.comm.*;  
import com.sun.comm.*;  


public class ListPorts  
{  
    public ListPorts()  
    {  
    }  
    
      
    private void getPorts()  
    {  
        System.out.println("getPorts called!!");  
        try  
        {  
        	
        	try{
		            Win32Driver w32Driver = new Win32Driver();  
		            System.out.println(" Instance created");
		            //Thread.sleep(3000);
		            
		            w32Driver.initialize();  
		            //Thread.sleep(3000);
		            System.out.println(" Initialised ");
        	}
        	catch(Exception e){
        		e.printStackTrace();
        	}
            // Get the list of ports on the machine.  
           
            Enumeration portList = (Enumeration) CommPortIdentifier.getPortIdentifiers();  
              
            CommPortIdentifier portId = null;  
              
            if( portList.hasMoreElements() == false)  
            {  
                System.out.println("No ports Available");  
            }  
            else  
            {  
                System.out.println("Listing ports : ");  
            }  
              
            while( portList.hasMoreElements() )  
            {  
                portId = (CommPortIdentifier) portList.nextElement();  
                  
                System.out.println("Port Name : "+portId.getName());  
                  
                // Print the type of the port : 1 - Serial , 2 - Parallel  
                //  
                if(portId.getPortType() == CommPortIdentifier.PORT_PARALLEL)  
                    System.out.println("\tType : "+portId.getPortType()+" Parallel Port ");  
                else if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL)  
                    System.out.println("\tType : "+portId.getPortType()+" Serial Port ");  
                // Print the current owner of the port if Available  
                //  
                if(portId.isCurrentlyOwned())  
                    System.out.println("\tOwner : "+portId.getCurrentOwner());  
                else  
                    System.out.println("\tOwner : Not Owned");  
                      
                System.out.println();  
            }  
        }  
        catch(Exception exp)  
        {  
        	exp.printStackTrace();
            //System.out.println(exp.getMessage());  
        }  
    }  
      
    public static void main( String a[] )   
    {  
        try  
        {  
            new ListPorts().getPorts();          
        }  
        catch(Exception exp)  
        {  
            System.out.println(exp.getMessage());  
        }  
    }  
}  